package com.financing.platform.service.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.financing.platform.dao.IAccountWithdrawDao;
import com.financing.platform.dao.IUserAccountDao;
import com.financing.platform.dto.AccountWithdrawDTO;
import com.financing.platform.dto.UserAccountDTO;
import com.financing.platform.dto.UserAccountDetailDTO;
import com.financing.platform.service.IAccountWithdrawService;
import com.financing.platform.util.Dict;
import com.financing.platform.vo.AccountWithdrawVO;


/**
 * @Title: BankCardServiceImpl.java
 * @Package com.financing.platform.service.impl
 * @Description: 银行卡service接口实现 
 * @author  lisu
 */
@Service("accountWithdrawServiceImpl")
public class AccountWithdrawServiceImpl extends BaseServiceImpl implements IAccountWithdrawService {
	
	private Logger log = Logger.getLogger(this.getClass());
	
	@Autowired
	private IAccountWithdrawDao accountWithdrawDaoImpl;
	
	@Autowired
	private IUserAccountDao userAccountDaoImpl;
	
	public List<AccountWithdrawVO> queryAccountWithdrawList(
			AccountWithdrawVO accountWithdrawVo) throws SQLException {
		return accountWithdrawDaoImpl.queryAccountWithdrawList(accountWithdrawVo);
	}

	@Override
	public int queryAccountWithdrawListCount(AccountWithdrawVO accountWithdrawVo)
			throws SQLException {
		return accountWithdrawDaoImpl.queryAccountWithdrawListCount(accountWithdrawVo);
	}
	public int updateAccountWithdraw(AccountWithdrawDTO accountWithdrawDTO)
			throws SQLException {
		
		//提现失败-更新信息
		if(Dict.WITHDRAW_STATUS_FAIL.equals(accountWithdrawDTO.getWithdrawStatus())){
			log.info("In updateAccountWithdraw WITHDRAW_STATUS_FAIL!!");
			
			//获取提现人信息
			AccountWithdrawVO accountWithdrawVo = accountWithdrawDaoImpl.queryAccountWithdrawInfo(accountWithdrawDTO.getId());
			if(accountWithdrawVo == null || !Dict.WITHDRAW_STATUS_ING.equals(accountWithdrawVo.getWithdrawStatus())){
				return 0;
			}
			
			log.info("In updateAccountWithdraw updateAccountBalanceActivity!!");
			//修改账户可提现余额
			UserAccountDTO userAccountDTO =  new UserAccountDTO();
			userAccountDTO.setId(accountWithdrawVo.getAccountId());
			userAccountDTO.setAccountBalanceActivity(accountWithdrawVo.getWithdrawAmount());//+可提现金额
			userAccountDTO.setAccountBalanceSleep(accountWithdrawVo.getWithdrawAmount());//-冻结金额
			userAccountDTO.setUpdateBy(accountWithdrawDTO.getUpdateBy());
			userAccountDTO.setUpdateDate(accountWithdrawDTO.getUpdateDate());
			userAccountDaoImpl.updateAccountBalanceActivity(userAccountDTO);
			
		}//提现成功
		else if(Dict.WITHDRAW_STATUS_SUCCESS.equals(accountWithdrawDTO.getWithdrawStatus())){
			log.info("In updateAccountWithdraw WITHDRAW_STATUS_SUCCESS!!");
			
			//获取提现人信息
			AccountWithdrawVO accountWithdrawVo = accountWithdrawDaoImpl.queryAccountWithdrawInfo(accountWithdrawDTO.getId());
			if(accountWithdrawVo == null || !Dict.WITHDRAW_STATUS_ING.equals(accountWithdrawVo.getWithdrawStatus())){
				return 0;
			}
			
			log.info("In updateAccountWithdraw updateAccountBalanceActivity!!");
			//修改账户可提现余额
			UserAccountDTO userAccountDTO =  new UserAccountDTO();
			userAccountDTO.setId(accountWithdrawVo.getAccountId());
			userAccountDTO.setAccountBalanceSleep(accountWithdrawVo.getWithdrawAmount());//-冻结金额
			userAccountDTO.setAccountBalance(accountWithdrawVo.getWithdrawAmount());//-总金额
			userAccountDTO.setUpdateBy(accountWithdrawDTO.getUpdateBy());
			userAccountDTO.setUpdateDate(accountWithdrawDTO.getUpdateDate());
			userAccountDaoImpl.updateAccountBalanceActivity(userAccountDTO);
			
			log.info("In updateAccountWithdraw  addUserAccountDetail!");
			//添加账户明显
			UserAccountDetailDTO  userAccountDetail = new UserAccountDetailDTO();
			userAccountDetail.setAmount(accountWithdrawVo.getWithdrawAmount()==null?null:accountWithdrawVo.getWithdrawAmount().negate());
			userAccountDetail.setFaId(accountWithdrawVo.getUserId());
			userAccountDetail.setCreateBy(accountWithdrawDTO.getUpdateBy());
			userAccountDetail.setCreateDate(accountWithdrawDTO.getUpdateDate());
			userAccountDetail.setUpdateBy(accountWithdrawDTO.getUpdateBy());
			userAccountDetail.setUpdateDate(accountWithdrawDTO.getUpdateDate());
			userAccountDetail.setAccountId(accountWithdrawVo.getAccountId());
			userAccountDetail.setActivitieGiftId(accountWithdrawVo.getId());//记录提现id
			userAccountDetail.setAccountType(Dict.P_ACCOUNT_DETAIL_TYPE_OUTPUT);
			userAccountDetail.setOperateType(Dict.P_ACCOUNT_OPERATE_CASH);
			userAccountDaoImpl.insertUserAccountDetail(userAccountDetail);
			
		}	
		return accountWithdrawDaoImpl.updateAccountWithdraw(accountWithdrawDTO);
	}

	public int insertAccountWithdraw(AccountWithdrawDTO accountWithdrawDTO)
			throws SQLException {
		return accountWithdrawDaoImpl.insertAccountWithdraw(accountWithdrawDTO);
	}

	public AccountWithdrawVO queryAccountWithdrawInfo(Integer id)
			throws SQLException {
		return accountWithdrawDaoImpl.queryAccountWithdrawInfo(id);
	}

	


}

	